﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using HYView;

namespace QuickModel.Command
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdLinkCad : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			CmdViewType types = (CmdViewType)13;
			bool flag = SwitchView.SetUsableView(types, cmdData) > 0;
			Result result;
			if (flag)
			{
				result = Autodesk.Revit.UI.Result.Failed;
			}
			else
			{
				RevitCommandId revitCommandId = RevitCommandId.LookupPostableCommandId(Autodesk.Revit.UI.PostableCommand.LinkCAD);
				try
				{
					bool flag2 = cmdData.Application.CanPostCommand(revitCommandId);
					if (flag2)
					{
						cmdData.Application.PostCommand(revitCommandId);
					}
				}
				catch (Autodesk.Revit.Exceptions.InvalidOperationException)
				{
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}
	}
}
